<?php

class FlinkAction extends BaseAction
{

    function __construct()
	{
		parent::__construct();
		$this->menuid = !empty($_GET['menuid']) ? intval($_GET['menuid']) : 13;
	}

	function Index()
	{
		$link_mod = D('flink');
		import("ORG.Util.Page");
		$count = $link_mod->count();
		$p = new Page($count,5);
		$link_list = $link_mod->limit($p->firstRow.','.$p->listRows)->order('ordid DESC')->findAll();
		$key = 1;
		foreach($link_list as $k=>$val){
			$link_list[$k]['key'] = ($p->nowPage-1) * ($p->listRows) + $key++;
		}
		$sub_menu = $this->admin_menu($this->menuid,true);
		$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=flink&a=add\', title:\''.L('add_flink').'\', width:\'440\', height:\'200\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('add_flink'));
		$page = $p->show();
		$this->assign('page',$page);
    	$this->assign('sub_menu',$sub_menu);
    	$this->assign('big_menu',$big_menu);
		$this->assign('link_list',$link_list);
		$this->display();
	}

	function add()
	{
		$this->display();
	}

	function edit()
	{
		$flink_mod = D('flink');
		if( isset($_GET['id']) ){
			$flink_id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : $this->error('请选择要编辑的链接');
		}
		$flink_info = $flink_mod->where('id='.$flink_id)->find();
		$this->assign('flink_info',$flink_info);
		$this->display();
	}

	function update()
	{
		$flink_mod = D('flink');
		$data = $flink_mod->create();
		if (!empty($_FILES['logo']['name'])) {
	        $data['logo'] = $this->_upload();
	    }
		$result = $flink_mod->where("id=".$data['id'])->save($data);
		if(false !== $result){
			$this->success(L('operation_success'), '', '', 'edit');
		}else{
			$this->error(L('operation_failure'));
		}
	}

	function insert()
	{
		$flink_mod = D('flink');
    	$data = array();
    	$name = isset($_POST['name']) && trim($_POST['name']) ? trim($_POST['name']) : $this->error(L('input').L('flink_name'));
    	$url = isset($_POST['url']) && trim($_POST['url']) ? trim($_POST['url']) : $this->error(L('input').L('flink_url'));
    	$exist = $flink_mod->where("url='".$url."'")->count();
		if($exist != 0){
			$this->error('该链接已经存在');
		}
    	$data = $flink_mod->create();
	    if ($_FILES['logo']['name']=='') {
	    }else{
	        $data['logo'] = $this->_upload();
	    }
    	$flink_mod->add($data);
    	$this->success(L('operation_success'), '', '', 'add');

	}

	function delete()
    {
		$flink_mod = D('flink');
		if((!isset($_GET['id']) || empty($_GET['id'])) && (!isset($_POST['id']) || empty($_POST['id']))) {
            $this->error('请选择要删除的链接！');
		}
		if( isset($_POST['id'])&&is_array($_POST['id']) ){
			$flink_ids = implode(',',$_POST['id']);
			$flink_mod->delete($flink_ids);
		}else{
			$flink_id = intval($_GET['id']);
		    $flink_mod->where('id='.$flink_id)->delete();
		}
		$this->success(L('operation_success'));
    }



	function _upload()
    {
    	import("ORG.Util.UploadFile");
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize = 3292200;
        $upload->allowExts = explode(',', 'jpg,gif,png,jpeg');
        $upload->savePath = './Public/Uploads/';
        //设置需要生成缩略图，仅对图像文件有效
        $upload->thumb = true;
        // 设置引用图片类库包路径
        $upload->imageClassPath = 'ORG.Util.Image';
        //设置需要生成缩略图的文件后缀
        $upload->thumbPrefix = 'm_,s_';  //生产2张缩略图
        //设置缩略图最大宽度
        $upload->thumbMaxWidth = '100';
        //设置缩略图最大高度
        $upload->thumbMaxHeight = '33';
        //设置上传文件规则
        $upload->saveRule = uniqid;
        //删除原图
        $upload->thumbRemoveOrigin = true;
        if (!$upload->upload()) {
            //捕获上传异常
            $this->error($upload->getErrorMsg());
        } else {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
            import("@.ORG.Image");
            Image::thumb($uploadList[0]['savepath'].'i_'.$uploadList[0]['savename'],'m_'.$uploadList[0]['savename'],'',120,40,true);
            $_POST['image'] = $uploadList[0]['savename'];
        }
        $logo = $_POST['image'];
        return $logo;
    }

    function sort_order()
    {
    	$flink_mod = D('flink');
		if (isset($_POST['listorders'])) {
            foreach ($_POST['listorders'] as $id=>$sort_order) {
            	$data['ordid'] = $sort_order;
                $flink_mod->where('id='.$id)->save($data);
            }
            $this->success(L('operation_success'));
        }
        $this->error(L('operation_failure'));
    }
}
?>